package org.budo.support.lang.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;

/**
 * @author lmw
 */
public class ByteArray {
    private static final Logger log = Slf4j.getLogger();

    private ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    public byte[] getBytes() {
        return byteArrayOutputStream.toByteArray();
    }

    public void write(int _byte) {
        byteArrayOutputStream.write(_byte);
    }

    public ByteArray write(byte[] bytes) {
        try {
            byteArrayOutputStream.write(bytes);
            return this;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public ByteArray writeJoin(byte[][] values, byte[] separator) {
        if (null == values || values.length < 1) {
            log.error("#36 values is null or empty, values=" + values);
            return this;
        }

        for (int i = 0; i < values.length - 1; i++) {
            this.write(values[i]);
            this.write(separator);
        }

        this.write(values[values.length - 1]); // 最后一条

        return this;
    }

    @Override
    public String toString() {
        return super.toString() + ", byte_string=" + new String(this.getBytes());
    }
}